package com.web.service.impl;

import com.web.dao.StudentDAO;
import com.web.dao.impl.StudentDAOimpl;
import com.web.entity.PageVO;
import com.web.entity.Student;
import com.web.service.StudentService;

import java.util.List;

public class StudentServiceimpl implements StudentService {
    private StudentDAO studentDAO = new StudentDAOimpl();
    @Override
    public List<Student> findAllStudents() {

        List<Student> allStudents = studentDAO.selectAllStudents();
        if (allStudents != null) {
            return allStudents;
        }
        System.out.println("没有学生");
        return null;
    }

    @Override
    public PageVO findStudentPage(int current, int pageSize) {
        //查询学生的总数
        int count= studentDAO.selectStudentsCount();
        //分页查询学生  current 页-->start 行
        int start = (current - 1) * pageSize;
        List<Student> students = studentDAO.selectStudentsPage(start, pageSize);
        System.out.println(students);
        PageVO pageVO = new PageVO(current, pageSize, count, students);
        return pageVO;

    }

    @Override
    public List<Student> findByName(String name) {
        StudentDAOimpl studentDAOimpl = new StudentDAOimpl();
        List<Student> students = studentDAOimpl.selectStudentByName(name);
        if (students != null) {
            return students;
        }
        System.out.println("没有学生");
        return null;
    }


    @Override
    public Student findStudentById(int id) {
        StudentDAOimpl studentDAO = new StudentDAOimpl();
        System.out.println("查询成功");
        return studentDAO.selectStudentById(id);
    }

    @Override
    public void addStudent(Student student) {
        StudentDAOimpl studentDAO = new StudentDAOimpl();
        studentDAO.insertStudent(student);
        System.out.println("添加成功");
    }

    @Override
    public void updateStudentById(Student student) {
        StudentDAOimpl studentDAO = new StudentDAOimpl();
        studentDAO.updateStudentById(student);
        System.out.println("更新成功");

    }

    @Override
    public void removeStudentById(int id) {
        StudentDAOimpl student = new StudentDAOimpl();
        student.deleteStudentById(id);
        System.out.println("删除成功");

    }
}
